package org.wlw.web.servlet;

import org.wlw.web.anno.Resource;
import org.wlw.web.models.Column;
import org.wlw.web.models.User;
import org.wlw.web.service.IColumnService;
import org.wlw.web.utils.Contrants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/admin/column")
public class ColumnServlet extends BaseServlet{
    private IColumnService columnService;

    @Resource
    public void setColumnService(IColumnService columnService) {
        this.columnService = columnService;
    }

    public String index(HttpServletRequest request, HttpServletResponse response) {
        List<Column> columnList = columnService.find(null);
        handleColumns(columnList);
        request.setAttribute("columns",columnList);
        return "column/list";
    }

    private void handleColumns(List<Column> columnList) {
        for (Column column:columnList) {
            column.setTypeDesc(Contrants.COLUMN_TYPE.get(column.getType()));
            column.setTopNavDesc(Contrants.YES_OR_NO.get(column.getIsTopNav()));
        }
    }

    public String delete(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        columnService.delete(id);
        return this.getRedirectUrl("/admin/column");
    }

    public String add(HttpServletRequest request,HttpServletResponse response) {
        request.setAttribute("types",Contrants.COLUMN_TYPE);
        request.setAttribute("pcs",columnService.find(null));
        return "/column/add";
    }

    public String update(HttpServletRequest request,HttpServletResponse response) {
        request.setAttribute("types",Contrants.COLUMN_TYPE);
        List<Column> cs = columnService.find(null);
        Integer id = Integer.parseInt(request.getParameter("id"));
        //根据id获取Column对象
        Column c = columnService.load(id);
        //删除fs中c
        for(int i=0;i<cs.size();i++) {
            Column cc = cs.get(i);
            if(cc.getId()==c.getId()) {
                cs.remove(cc);
            }
        }
        request.setAttribute("column",c);
        request.setAttribute("pcs",columnService.find(null));
        return "/column/update";
    }

    public String addAction(HttpServletRequest request,HttpServletResponse response) {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String url = request.getParameter("url");
        Integer isTopNav = Integer.parseInt(request.getParameter("isTopNav"));
        Integer pid = Integer.parseInt(request.getParameter("pid"));
        Column c = new Column();
        c.setPid(pid);
        c.setName(name);
        c.setType(type);
        c.setIsTopNav(isTopNav);
        c.setUrl(url);
        columnService.add(c);
        return getRedirectUrl("/admin/column");
    }

    public String updateAction(HttpServletRequest request,HttpServletResponse response) {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String url = request.getParameter("url");
        Integer isTopNav = Integer.parseInt(request.getParameter("isTopNav"));
        Integer pid = Integer.parseInt(request.getParameter("pid"));
        int id = Integer.parseInt(request.getParameter("id"));
        Column c = columnService.load(id);
        c.setPid(pid);
        c.setName(name);
        c.setType(type);
        c.setIsTopNav(isTopNav);
        c.setUrl(url);
        columnService.update(c);
        return getRedirectUrl("/admin/column");
    }
}
